﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using Microsoft.SharePoint.Client.Services;
using System.Xml;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;


/*
Developed By: Jyoti Seth
Developed On: 15th Jul 2011
Description: The code provides all the projects info in xml format
*/
namespace SiteProjectsInfo
{
    [BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ProjectsInfo : IProjectsInfo
    {
        
        // Function will fetch all the project details and return it in xml format
        public XmlElement GetProjectSiteDetails()
        {
           
                string projectName = String.Empty;
                string creator = String.Empty;
                string owner = String.Empty;
                string contributor = String.Empty;
                string visitor = String.Empty;

                SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
                SPSiteCollection siteCollections = webApplication.Sites;

                XmlDocument XD = new XmlDocument();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                XmlElement root = XD.CreateElement("Projects");
                foreach (SPSite siteCollection in siteCollections)
                {

                    // Check if the url contains /projects/ a
                    if (siteCollection.Url.IndexOf("/projects/") > 1) 
                    {
                        XmlElement eleProject = XD.CreateElement("Project");
                        root.AppendChild(eleProject);

                        XmlElement eleProjectName = XD.CreateElement("ProjectName");
                        projectName = siteCollection.Url;
                        projectName = projectName.Substring(siteCollection.Url.IndexOf("/projects/") + 10);
                        eleProjectName.InnerText = projectName;
                        eleProject.AppendChild(eleProjectName);

                        XmlElement eleCreator = XD.CreateElement("Creator");
                        creator = siteCollection.Owner.LoginName;
                        creator = creator.Replace("i:0#.w|", "");
                        creator = creator.Replace("i:0#.f|sqlmember|", "");
                        eleCreator.InnerText = creator;
                        eleProject.AppendChild(eleCreator);

                        XmlElement eleOwners = XD.CreateElement("Owners");
                        XmlElement eleContributors = XD.CreateElement("Contributors");
                        XmlElement eleVisitors = XD.CreateElement("Visitors");
                        eleProject.AppendChild(eleOwners);
                        eleProject.AppendChild(eleContributors);
                        eleProject.AppendChild(eleVisitors);

                        using (SPSite site = new SPSite(siteCollection.Url))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                // Following code is written to find the groupid of the current site members.
                                SPGroupCollection groups = web.Groups;
                                string groupName;
                                int groupId = 0;
                                foreach (SPGroup group in groups) // iterates through all the site groups
                                {
                                    groupName = group.Name;
                                    if (groupName.IndexOf("Owners") > 0) // If the Group includes owner
                                    {
                                        groupId = group.ID;
                                        foreach (SPUser singleUser in group.Users)
                                        {
                                            XmlElement eleOwner = XD.CreateElement("Owner");
                                            owner = singleUser.LoginName;
                                            owner = owner.Replace("i:0#.w|", "");
                                            owner = owner.Replace("i:0#.f|sqlmember|", "");
                                            eleOwner.InnerText = owner;
                                            eleOwners.AppendChild(eleOwner);
                                        }

                                    }
                                    if (groupName.IndexOf("Members") > 0) // If the Group includes members
                                    {
                                        groupId = group.ID;
                                        foreach (SPUser singleUser in group.Users)
                                        {
                                            XmlElement eleContributor = XD.CreateElement("Contributor");
                                            contributor = singleUser.LoginName;
                                            contributor = contributor.Replace("i:0#.w|", "");
                                            contributor = contributor.Replace("i:0#.f|sqlmember|", "");
                                            eleContributor.InnerText = contributor;
                                            eleContributors.AppendChild(eleContributor);
                                        }
                                    }
                                    if (groupName.IndexOf("Visitors") > 0) // If the Group includes visitors
                                    {
                                        groupId = group.ID;
                                        foreach (SPUser singleUser in group.Users)
                                        {
                                            XmlElement eleVisitor = XD.CreateElement("Visitor");
                                            visitor = singleUser.LoginName;
                                            visitor = visitor.Replace("i:0#.w|", "");
                                            visitor = visitor.Replace("i:0#.f|sqlmember|", "");
                                            eleVisitor.InnerText = visitor;
                                            eleVisitors.AppendChild(eleVisitor);
                                        }
                                    }
                                }
                            }
                        }

                    }


                }
                XD.AppendChild(root);
                                
            });
            return XD.DocumentElement;
        }
    }
}
